﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenChart.Entities;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace OpenChart.PublicUnits
{
    public static class ChartHelper
    {
        /// <summary>
        /// 获取坐标间隔值
        /// </summary>
        /// <param name="customerInterval">客户指定的间隔值</param>
        /// <param name="intervalType">客户指定的间隔方式</param>
        /// <returns>真实间隔值</returns>
        public static double GetInterval(double customerInterval, Enums.InervalType intervalType)
        {
            switch (intervalType)
            {
                case Enums.InervalType.Number:
                    return customerInterval;
                case Enums.InervalType.Seconds:
                    return customerInterval / 24 / 60 / 60;
                case Enums.InervalType.Minutes:
                    return customerInterval / 24 / 60;
                case Enums.InervalType.Hours:
                    return customerInterval / 24;
                case Enums.InervalType.Days:
                    return customerInterval;
                case Enums.InervalType.Months:
                    return customerInterval * 30;
                case Enums.InervalType.years:
                    return customerInterval * 365;
                default:
                    return -1;
            }
        }

        /// <summary>
        /// 获取线条样式
        /// </summary>
        /// <param name="style">线条样式名称</param>
        /// <returns>线条样式</returns>
        public static DashStyle GetDashStyle(OpenChart.Entities.Enums.LineStyle style)
        {
            switch (style)
            {
                case Enums.LineStyle.Solid:
                    return DashStyle.Solid;
                case Enums.LineStyle.Dash:
                    return DashStyle.Dash;
                case Enums.LineStyle.Dot:
                    return DashStyle.Dot;
                case Enums.LineStyle.DashDot:
                    return DashStyle.DashDot;
                case Enums.LineStyle.DashDotDot:
                    return DashStyle.DashDotDot;
                default:
                    return DashStyle.Solid;
            }
        }

        /// <summary>
        /// 根据间隔类型获取坐标显示值
        /// </summary>
        /// <param name="customerValue">未经过处理的值</param>
        /// <param name="type">间隔类型</param>
        /// <param name="timeFomart">如果是时间类型，可以设置时间显示格式</param>
        /// <returns>坐标显示值</returns>
        public static string GetAxisLable(double customerValue, Enums.InervalType type, string timeFomart)
        {
            if (type == Enums.InervalType.Number)
            {
                return customerValue.ToString();
            }
            else
            {
                if (timeFomart != null)
                {
                    return DateTime.FromOADate(customerValue).ToString(timeFomart);
                }
                else
                {
                    return DateTime.FromOADate(customerValue).ToString();
                }
            }
        }

        /// <summary>
        /// 根据间隔类型获得浮点型数据
        /// </summary>
        /// <param name="value">源数据</param>
        /// <param name="type">间隔类型</param>
        /// <returns>结果数据</returns>
        public static double GetDoubleValue(object value, Enums.InervalType type)
        {
            if (type == Enums.InervalType.Number)
            {
                return Convert.ToDouble(value);
            }
            else
            {
                return Convert.ToDateTime(value).ToOADate();
            }
        }

        /// <summary>
        /// 获取随机颜色
        /// </summary>
        /// <returns>颜色</returns>
        public static Color GetRandomColor()
        {
            int seed = DateTime.Now.Millisecond;
            Random random = new Random(seed);
            int r1 = random.Next(0, 256);
            int r2 = random.Next(0,256);
            int r3 = random.Next(0, 256);
            return Color.FromArgb(r1, r2, r3);
        }
    }
}
